package com.techsen.tsweb.sys.web.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.techsen.tsweb.core.domain.JsonResult;
import com.techsen.tsweb.core.util.SubjectUtil;
import com.techsen.tsweb.core.util.TreeUtil;
import com.techsen.tsweb.core.util.ValidUtil;
import com.techsen.tsweb.sys.domain.Menu;
import com.techsen.tsweb.sys.domain.User;
import com.techsen.tsweb.sys.service.MenuService;
import com.techsen.tsweb.sys.util.SysConst;

@Controller
@RequestMapping("/sys/menu")
public class MenuController {

    @Resource
    private MenuService menuService;
    
    /**
     * 查询所有菜单
     */
    @RequestMapping("/list")
    public @ResponseBody List<Menu> list(boolean tree) {
        List<Menu> menus = this.menuService.findAll();
        if (tree) {
            menus = TreeUtil.toTree(menus);
        }
        return menus;
    }
    
    /**
     * 根据用户ID获取授权给该用户的所有菜单
     */
    @RequestMapping("/list-authorized-menus")
    public @ResponseBody List<Menu> listAuthorizedMenusByUserId(boolean tree) {
        User user = (User) SubjectUtil.getSubject().getSession().getAttribute(SysConst.LOGIN_USER);
        List<Menu> menus = this.menuService.getAuthorizedMenusByUserId(user.getId());
        if (tree) {
            menus = TreeUtil.toTree(menus);
        }
        return menus;
    }
    
    /**
     * 进入菜单管理界面
     */
    @RequestMapping
    public String index(String menuId, Model model) {
        model.addAttribute("menuId", menuId);
        return "sys/menu";
    }
    
    /**
     * 查看菜单
     */
    @RequestMapping("/{id}")
    public String view(@PathVariable("id") String id, Model model) {
        if (ValidUtil.isValid(id)) {
            Menu menu = this.menuService.get(id);
            model.addAttribute("menu", menu);
        }
        return "sys/menu-view";
    }
    
    /**
     * 根据菜单ID查询所有子菜单集合
     */
    @RequestMapping("/children/{id}")
    public @ResponseBody List<Menu> children(boolean tree,
            @PathVariable("id") String id) {
        List<Menu> children = this.menuService.getAllChildrenMenu(id);
        if (tree) {
            children = TreeUtil.toTree(children);
        }
        return children;
    }
    
    /**
     * 进入添加菜单界面
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(String parentId, Model model) {
        model.addAttribute("parentId", parentId);
        return "sys/menu-add";
    }
    
    /**
     * 添加菜单
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody JsonResult add(Menu menu) {
        this.menuService.add(menu);
        return new JsonResult(menu);
    }
    
    /**
     * 删除菜单
     */
    @RequestMapping("/delete")
    public @ResponseBody JsonResult delete(String id) {
        if (ValidUtil.isValid(id)) {
            this.menuService.delete(id);
        }
        return new JsonResult(id);
    }
    
    /**
     * 进入修改菜单界面
     */
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(String menuId, Model model) {
        Menu menu = this.menuService.get(menuId);
        model.addAttribute("menu", menu);
        return "sys/menu-update";
    }
    
    /**
     * 修改菜单
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public @ResponseBody JsonResult update(Menu menu) {
        this.menuService.update(menu);
        return new JsonResult(menu);
    }
    
    /**
     * 根据组织类型列出所有父组织类型的所有组织
     */
    @RequestMapping("list-available-parents-by-id")
    public @ResponseBody List<Menu> listAvailableParentsById(boolean tree, String menuId) {
        List<Menu> menus = new ArrayList<Menu>();
        Menu childMenu = this.menuService.get(menuId);
        if (childMenu != null) {
            menus = this.menuService.findAll();
            List<String> childrenMenuIds = this.menuService.getAllChildrenMenuId(menuId);
            if (ValidUtil.isValid(childrenMenuIds)) {
                Iterator<Menu> iter = menus.iterator();
                while (iter.hasNext()) {
                    Menu menu = iter.next();
                    if (ValidUtil.isValid(menu.getPath()) || childrenMenuIds.contains(menu.getId())) {
                        iter.remove();
                    }
                }
            }
        }
        if (tree) {
            menus = TreeUtil.toTree(menus);
        }
        return menus;
    }
    
}
